const app = require('http').createServer();
const fs = require('fs');
const CONFIG = require('./config');
const controller = require('./controller');
const path = require('path');
app.on('request', (req, res) => {
    /* 跨域 */
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Methods', '*');
    /* 处理请求 */
    let { method, url } = req;
    console.log(method, url)
    method = method.toLowerCase();
    if (method === "post") {
        /* 上传 */
        if (url === '/multi') {
            controller.multicleUpload(req, res);
        }
    }
    else if (method === 'options') {
        res.end();
    }
    else if (method === 'get') {
        if (url.startsWith('/static/')) {
            fs.readFile(path.join(__dirname, url), (err, data) => {
                if (err)
                    return res.end(JSON.stringify({ msg: err }));
                res.end(data);
            })
        }
    }
})
app.listen(CONFIG.port, CONFIG.host, () => {
    console.log(`Server start at ${CONFIG.host}:${CONFIG.port}`);
})


